package com.yunshipei.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseIntentBuilder;
import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.google.zxing.activity.CaptureActivity.INTENT_EXTRA_KEY_QR_SCAN;
import static com.yunshipei.ui.activity.MainActivity.ACTION_QR_CODE_DATA_RESULT;
import static com.yunshipei.ui.activity.MainActivity.EXTRA_QR_CODE_CONTENT_IS_ADAPTER;

public class QRScanResultActivity extends BaseActivity {

    private static final String EXTRA_RESULT = "extra.scan.result";

    @Bind(R.id.tv_result)
    protected TextView tvResult;
    @Bind(R.id.tv_copy_result)
    protected Button mCopyBtn;
    @Bind(R.id.tv_open_result)
    protected Button mOpenBtn;

    private String mResult = "";
    private static final String HOST_MATCHER = "^(?:[a-z0-9]\\.|[a-z0-9][-a-z0-9]{0,61}[a-z0-9]\\.)+(?:com\\.cn|cn|com|net|org|edu|gov|mil|arts|firm|info|nom|rec|store|web|au|jp|me|io)";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrscan_result);
        ButterKnife.bind(this);
        int color = EnterConfig.getInstance().getGlobalColor();
        mCopyBtn.setBackgroundColor(color);
        mOpenBtn.setBackgroundColor(color);
        mOpenBtn.setVisibility(View.GONE);
        mResult = getIntent().getStringExtra(EXTRA_RESULT);
        tvResult.setText(mResult);
        if (!TextUtils.isEmpty(mResult)) {
            try {
                if (mResult.matches(HOST_MATCHER)) {
                    mOpenBtn.setVisibility(View.VISIBLE);
                }
            } catch (Exception ignored) {
            }
        }
    }

    @OnClick({R.id.tv_copy_result, R.id.tv_open_result})
    protected void copyResult(View view) {
        switch (view.getId()) {
            case R.id.tv_copy_result:
                ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("Label", mResult);
                cmb.setPrimaryClip(clipData);
                ToastUtils.showToast("复制成功");
                break;
            case R.id.tv_open_result:
                String url = "http://" + mResult;
                Intent intent = new Intent(this, MainActivity.class).setAction(ACTION_QR_CODE_DATA_RESULT).putExtra(INTENT_EXTRA_KEY_QR_SCAN, url).putExtra(EXTRA_QR_CODE_CONTENT_IS_ADAPTER, false);
                startActivity(intent);
                break;
        }
    }

    public static final class ScanResultIntentBuilder extends BaseIntentBuilder {

        public ScanResultIntentBuilder(Context context) {
            super(context);
        }

        public ScanResultIntentBuilder setData(String result) {
            getIntent().putExtra(EXTRA_RESULT, result);
            return this;
        }

        @Override
        protected Class<?> getTargetClazz() {
            return QRScanResultActivity.class;
        }
    }
}
